package com.cys.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cys.bean.SoilData;
import com.cys.db.DBHelper;

/**
 * Servlet implementation class HSoilDataServlet
 */
@WebServlet("/HSoilDataServlet")
public class HSoilDataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HSoilDataServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			// Connection conn = DriverManager.getConnection(url, username,
			// password);
			Connection conn = DBHelper.getConn();
			// 鑾峰彇Statement
			Statement stmt = conn.createStatement();
			// 娣诲姞鍥句功淇℃伅鐨凷QL璇彞
			String sql = "select * from t_data_dev_soil_monitor order by data_time limit 10";
			// 鎵ц鏌ヨ
			ResultSet rs = stmt.executeQuery(sql);
			// 瀹炰緥鍖朙ist瀵硅薄
			List<SoilData> slist = new ArrayList<SoilData>();
			// 鍒ゆ柇鍏夋爣鍚戝悗绉诲姩锛屽苟鍒ゆ柇鏄惁鏈夋晥
			while (rs.next()) {
				// 瀹炰緥鍖朆ook瀵硅薄
				SoilData soilData = new SoilData();
				soilData.setDev_soil_monitor(rs.getInt("dev_soil_monitor"));
				soilData.setData_time(rs.getString("data_time"));
				soilData.setOil_wetness(rs.getFloat("soil_wetness"));
				soilData.setSoil_salinity(rs.getFloat("soil_salinity"));
				// 灏嗗浘涔﹀璞℃坊鍔犲埌闆嗗悎涓�
				slist.add(soilData);
			}
			// 灏嗗浘涔﹂泦鍚堟斁缃埌request涔嬩腑
			request.setAttribute("slist", slist);
			rs.close(); // 鍏抽棴ResultSet
			stmt.close(); // 鍏抽棴Statement
			conn.close(); // 鍏抽棴Connection
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 璇锋眰杞彂鍒癰ook_list.jsp
		request.getRequestDispatcher("HistoricalData.jsp").forward(request, response);
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
